package com.example.controller;

import com.example.bean.Software;
import com.example.exception.TestNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class TestController {

    @Autowired
    private Software software;

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        System.out.println("software:" + software);
        throw new TestNotFoundException();
    }

    // 这样子，handleException() 方法就能处理本控制器中的所有TestNotFoundException了
    @ExceptionHandler(TestNotFoundException.class)
    public void handleException(HttpServletResponse response) {
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("+++++++++++++++++++++++++");
        try {
            response.getWriter().println("发生错误了哦");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
